﻿/**
 * 
 *  解析收到电视发过来的信息
 * 
 * 
 * 
 * 
 */

using NewTools.Properties;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace NewTools
{
    internal class CompareResult
    {
        private static CompareResult _instance;
        public static CompareResult GetInstance()
        {
            if (_instance == null)
                _instance = new CompareResult();
            return _instance;
        }

        public CompareResult()
        {
        }
        public string  RunFunctionSchedule(string functionName,string receiveData)
        {
            CompareResult compareResult = Activator.CreateInstance(typeof(CompareResult),true) as CompareResult;
            MethodInfo method = compareResult.GetType().GetMethod(functionName);
            string retValue =(string) method.Invoke(this, new object[]{
                receiveData 
            });
            return retValue;
        }

        public string CompareNormalSetValue(string receiveData)
        {
            string[] commandList = receiveData.Split(' ');
            if (commandList[1].Equals("B0"))
                return Properties.Resources.Success;
            return Properties.Resources.Fail;
        }

        public string CompareNormalGetValue(string receiveData)
        {
            string[] commandList = receiveData.Split(' ');
            if (commandList[1].Equals("B2"))
            {
                Int32 value = Convert.ToInt32( commandList[4],16);
                return Convert.ToString(value) + "    " +  Properties.Resources.Success;
            }   
            return Properties.Resources.Fail;
        }

        public string CompareGetPictureMode(string receiveData)
        {
            string[] commandList = receiveData.Split(' ');
            if (commandList[1].Equals("B2"))
            {
                string value = commandList[3];
                if (value.Equals("03"))
                    value = Properties.Resources.PictureModeUser;
                else if (value.Equals("02"))
                    value = Properties.Resources.PictureModeSoft;
                else if (value.Equals("01"))
                    value = Properties.Resources.PictureModeBright;
                else if (value.Equals("00"))
                    value = Properties.Resources.PictureModeStandard;
                
                return  value + "    " + Properties.Resources.Success; ;
            }
            return Properties.Resources.Fail;
        }

        public string CompareUsbNum(string receiveData)
        {
            string[] commandList = receiveData.Split(' ');
            if (commandList[1].Equals("B2"))
            {
                string value = commandList[3];
                return value + "    " + Properties.Resources.Success;
            }
            return Properties.Resources.Fail;
        }

        public string CompareTvIP(string receiveData)
        {
            Console.WriteLine(receiveData);
            string[] commandList = receiveData.Split(' ');
            if (commandList[1].Equals("B2"))
            {
                Int32 a = Convert.ToInt32(commandList[3],16);
                Int32 b = Convert.ToInt32(commandList[4], 16);
                Int32 c = Convert.ToInt32(commandList[5], 16);
                Int32 d = Convert.ToInt32(commandList[6], 16);

                return Convert.ToString(a) + "." + Convert.ToString(b) + "." + Convert.ToString(c) +
                    "." + Convert.ToString(d);
            }
            return Properties.Resources.Fail;
        }

        public string CompareWifiNum(string receiveData)
        {
            string[] commandList = receiveData.Split(' ');
            if (commandList[1].Equals("B2"))
            {
                Int32 value = Convert.ToInt32(commandList[4], 16);
                return Convert.ToString(value) + "    " + Properties.Resources.Success;
            }
            return Properties.Resources.Fail;
        }

        public string CompareIsHaveBlueTooth(string receiveData)
        {
            string[] commandList = receiveData.Split(' ');
            if (commandList[1].Equals("B2"))
            {
                Int32 value = Convert.ToInt32(commandList[4], 16);

                return value == 1 ? "支持蓝牙" : "不支持蓝牙"; 
            }
            return Properties.Resources.Fail;
        }

        public string CompareTvMacAdress(string receiveData)
        {
            string[] commandList = receiveData.Split(' ');
            string mac = "";
            if (commandList[1].Equals("B2"))
            { 
                for (Int32 i=0;i<6;i++)
                {
                    mac += commandList[i + 3];
                    if (i == 5)
                        continue;
                    mac += ":";
                }
                return mac;
            }
            return Properties.Resources.Fail;
        }

        public string CompareTvBuildTime(string receiveData)
        {
            string[] commandList = receiveData.Split(' ');
            int num = Convert.ToInt32(commandList[2]);
            string time = "";
            if (commandList[1].Equals("B2"))
            {
                byte[] date = new byte[3] { Convert.ToByte(commandList[3], 16),
                     Convert.ToByte(commandList[4], 16), Convert.ToByte(commandList[5],16)};

                for(Int32 i = 0; i < 3; i++)
                {
                    time += Convert.ToString(Convert.ToInt32(commandList[i + 7],16));
                    if (i == 2)
                        continue;
                    time += ":";
                }
                return Encoding.ASCII.GetString(date) + " " + Convert.ToString(Convert.ToInt32(commandList[6]))+ " " + time;
            }
            return Properties.Resources.Fail;
        }

        public string CompareGetTVInputType(string receiveData)
        {
            string[] commandList = receiveData.Split(' ');
            if (commandList[1].Equals("B2"))
            {
                Int32 Value = Convert.ToInt32(commandList[3]);
                string sourceName = "";
                switch (Value) 
                {
                    case 9:
                        sourceName = "HDMI3";
                        break;
                    case 8:
                        sourceName = "HDMI2";
                        break;
                    case 7:
                        sourceName = "HDMI1";
                        break;
                    case 3:
                        sourceName = "AV";
                        break;
                    case 2:
                        sourceName = "DTV";
                        break;
                    case 1:
                        sourceName = "ATV";
                        break;

                }

                return sourceName + "    " + Properties.Resources.Success;
            }
            return Properties.Resources.Fail;
        }

    }
}
